package logic

import (
	"context"
	"encoding/json"
	"goods-sys/models/cache"
	"goods-sys/models/mysql"
	"time"

	"goods-sys/goodsrpc/goodsrpc"
	"goods-sys/goodsrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ListPingLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewListPingLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListPingLogic {
	return &ListPingLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 列表
func (l *ListPingLogic) ListPing(in *goodsrpc.ListRequest) (*goodsrpc.ListResponse, error) {
	// todo: add your logic here and delete this line

	var data []*mysql.User
	key := "key_list"
	erront, _ := cache.Rdb.Get(l.ctx, key).Result()
	if erront == "" {
		mysql.DB.Find(&data).Debug()
		jsonData, _ := json.Marshal(data)
		cache.Rdb.Set(l.ctx, key, jsonData, time.Minute*120)
	} else {
		json.Unmarshal([]byte(erront), &data)
	}

	var List []*goodsrpc.Userlsit

	for _, shop := range data {
		lists := goodsrpc.Userlsit{
			UserId: int64(shop.ID),
			Title:  shop.Title,
			Post:   shop.Post,
			Shu:    shop.Shu,
			Pole:   shop.Pole,
		}
		List = append(List, &lists)
	}

	return &goodsrpc.ListResponse{
		List: List,
	}, nil
}
